package org.will.cqrs.command;

import org.reveno.atp.api.Reveno;
import org.will.common.RevenoHelper;
import org.will.cqrs.event.Event;

import java.util.List;

/**
 * Created by EdifierWill on 2016/8/11.
 */
public class CommandFactory<TCommand extends Command,TCommandAction extends CommandAction,TEvent extends Event> {

    public void MakeCommand(List<Class<TCommand>> cmds,List<Class<TCommandAction>> actions,List<Class<TEvent>> events)
    {
        Reveno reveno = RevenoHelper.getInstance().getReveno();

        for(Class<TCommand> cmd:cmds){
            reveno.domain().command(cmd, (c, ctx) -> {
                try {
                    c.handleCommand(c, ctx);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });
        }

        for(Class<TCommandAction> action:actions){
            reveno.domain().transactionAction(action, (a, ctx) -> {
                try {
                    a.handler(a, ctx);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });
        }

        for(Class<TEvent> event:events){
            reveno.events().eventHandler(event, (e, m) -> {
                try {
                    e.eventHandler(reveno, m);
                } catch (Exception e1) {
                    e1.printStackTrace();
                }
            });
        }
    }
}
